//
//  String+MD5.swift
//  tripartiteTestDemo
//
//  Created by rimi on 2017/5/19.
//  Copyright © 2017年 rimi. All rights reserved.
//

import Foundation


extension String{
    
    /// 将字符串进行MD5加密
    /// 使用这个分类必须要添加桥接文件导入OC的头文件  #import <CommonCrypto/CommonDigest.h>
    /// - Returns: MD5加密字符串
    func md5() ->String {
        let str = cString(using: .utf8)
        let strLen = CUnsignedInt(lengthOfBytes(using: .utf8))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
        CC_MD5(str!, strLen, result)
        var hash = ""
        for i in 0 ..< digestLen {
            hash += String(format: "%02x", result[i])
        }
        result.deinitialize()
        return hash
    }
}
